##########################################################################################################################################
###### This is the file containing the main panel regression results
###############################
# It creates the final results for Tables 1, 2, 3, 4, 7, 9, 10, and Figure 1
# Please note that in some instances, the paper contains some formatting modifications
# such as a "log()" that is not automatically added here or that we present the negative
# of tsim, while most output here is generated for the positive

# This is just the final step. There are other code files that are used for data preparation
# This processed data are saved and loaded in this code

# set to your working directory:
setwd("C:\\Users\\Admin\\OneDrive\\Desktop\\Market Power and Systematic Risk - Data and Code")
getwd()


########################################################
### Table 9: Excess Return Prediction

load("PANEL_NAICS.RData")
#install.packages("stringi")
require(stringi)
#install.packages("lfe")
require(lfe)
require(data.table)
PANEL_NAICS$year <- as.numeric(unlist(lapply(strsplit(as.character(PANEL_NAICS$Time),split=""),function(x) stri_paste(head(x,n=4),collapse=""))))
PANEL_NAICS = PANEL_NAICS[!duplicated(paste0(PANEL_NAICS$PERMNO,PANEL_NAICS$Time)),]

########################### Univariate ###########################
load("Control_Panel.RData")
#Build the dataframe for HHI with all beta specifications:
Market_Power = data.frame(Beta_M = rep(NA,20), Beta_CF = rep(NA,20), Beta_DR = rep(NA,20), Beta_Up = rep(NA,20), Beta_Down = rep(NA,20),
                          Weighting_Beta_M = rep(NA,20), Weighting_Beta_CF = rep(NA,20), Weighting_Beta_DR = rep(NA,20), Weighting_Beta_Up = rep(NA,20), Weighting_Beta_Down = rep(NA,20),
                          Shrinking_Beta_M = rep(NA,20), Shrinking_Beta_CF = rep(NA,20), Shrinking_Beta_DR = rep(NA,20), Shrinking_Beta_Up = rep(NA,20), Shrinking_Beta_Down = rep(NA,20),
                          Weighting_Shrinking_Beta_M = rep(NA,20), Weighting_Shrinking_Beta_CF = rep(NA,20), Weighting_Shrinking_Beta_DR = rep(NA,20), Weighting_Shrinking_Beta_Up = rep(NA,20), Weighting_Shrinking_Beta_Down = rep(NA,20))

rownames(Market_Power) = c("HHI","t-stat 1","HHI^2","t-stat 1.1","R_sq 1","prodmktfluid","t-stat 2","prodmktfluid^2","t-stat","R_sq 2",
                           "tnic3hhi","t-stat 3","tnic3hhi^2","t-stat 3.1","R_sq 3","tnic3simm","t-stat 4","tnic3simm^2","t-stat 4.1","R_sq 4")

#install.packages("R.matlab")
library(R.matlab)

# The file here is a txt file containing the monthly returns of all companies. The data
# are downloaded from CRSP, adjusted for delistings as in Warther (1995) and Shumway and Warther (1999)
# and saved. Important: The file already contains the returns over the NEXT month.
# The format of the txt file is Date ("yyyymmdd"), Permno, Nextexret (in decimals)
mat_data <- read.delim("Nextret_monthly.txt",sep=",")

mat_data$Date=as.Date(as.character(mat_data$Date), format = "%Y%m%d")
Temp = Panel_New[,c("Time","YearMonth","year","NAICS","PERMNO","Beta_M","Beta_CF","Beta_DR","Beta_Down","Beta_Up","Weight_Beta_M","Weight_Beta_CF","Weight_Beta_DR",
                    "Weight_Beta_Up","Weight_Beta_Down","Shrinking_Beta_M","Shrinking_Beta_CF","Shrinking_Beta_DR","Shrinking_Beta_Up","Shrinking_Beta_Down",
                    "Weighting_Shrinking_Beta_M","Weighting_Shrinking_Beta_CF","Weighting_Shrinking_Beta_DR","Weighting_Shrinking_Beta_Up","Weighting_Shrinking_Beta_Down","HHI","prodmktfluid","tnic3hhi","tnic3tsimm",
                    "Age","Momentum","Illiquidity","MktCap","Mkt_Book","Default_Spread","Financial_Leverage", "Operating_Leverage","Firm_size","InvestmentRates","ROE","Logat","Dividend_Dummy","q","I_Vol")]#
for(i in 1:dim(Temp)[2]){
  Temp[,i][Temp[,i] == Inf] = NA
  Temp[,i][Temp[,i] == -Inf] = NA
}
Temp = na.omit(Temp)

Temp_ret = Panel_New[,c("Time","YearMonth","year","PERMNO","HHI","prodmktfluid","tnic3hhi","tnic3tsimm",
                    "Age","Momentum","Illiquidity","MktCap","Mkt_Book","Default_Spread","Financial_Leverage", "Operating_Leverage","Firm_size","InvestmentRates","ROE","Logat","Dividend_Dummy","q","I_Vol")]#
for(i in 1:dim(Temp_ret)[2]){
  Temp_ret[,i][Temp_ret[,i] == Inf] = NA
  Temp_ret[,i][Temp_ret[,i] == -Inf] = NA
}
Temp_ret = na.omit(Temp_ret)
Temp_ret = merge(Temp_ret,mat_data,by.x=c("Time","PERMNO"),by.y=c("Date","Permno"))
Temp_ret = na.omit(Temp_ret)

###
Temp_ret$HHI=scale(log(Temp_ret$HHI))
a<-poly(Temp_ret$HHI,2)
Temp_ret$HHI2=a[,2]/sd(a[,2])
Temp_ret$prodmktfluid=scale(Temp_ret$prodmktfluid)
a<-poly(Temp_ret$prodmktfluid,2)
Temp_ret$prodmktfluid2=a[,2]/sd(a[,2])
Temp_ret$tnic3hhi=scale(Temp_ret$tnic3hhi)
a<-poly(Temp_ret$tnic3hhi,2)
Temp_ret$tnic3hhi2=a[,2]/sd(a[,2])
Temp_ret$tnic3tsimm=scale(Temp_ret$tnic3tsimm)
a<-poly(Temp_ret$tnic3tsimm,2)

Temp_ret$tnic3tsimm2=a[,2]/sd(a[,2])
Temp_ret$Age=scale(log(Temp_ret$Age))
Temp_ret$Momentum=scale(Temp_ret$Momentum)
Temp_ret$Illiquidity=scale(Temp_ret$Illiquidity)
Temp_ret$MktCap=scale(log(Temp_ret$MktCap))
Temp_ret$Mkt_Book=scale(log(Temp_ret$Mkt_Book))
Temp_ret$Financial_Leverage=scale(Temp_ret$Financial_Leverage)
Temp_ret$Operating_Leverage=scale(Temp_ret$Operating_Leverage)
Temp_ret$Default_Spread=scale(Temp_ret$Default_Spread)
Temp_ret$Firm_size=scale(Temp_ret$Firm_size)
Temp_ret$InvestmentRates=scale(Temp_ret$InvestmentRates)
Temp_ret$ROE=scale(Temp_ret$ROE)
Temp_ret$Logat=scale(Temp_ret$Logat)
Temp_ret$q=scale(Temp_ret$q)
Temp_ret$I_Vol=scale(Temp_ret$I_Vol)

Temp_ret$Nextexret=Temp_ret$Nextexret*100

# Fama-MacBeth regressions
# Load required packages
#install.packages("dplyr")
library(dplyr)
library(sandwich)
library(lmtest)
library(DescTools)
# Prepare the data
# Assume you have a data frame named "data" with the dependent variable (Y) and independent variables (X1, X2, etc.) for each time period.
# Split the data into cross-sectional periods
data_split <- split(Temp_ret, Temp_ret$Time)  # Assuming "Time" is the column representing different time periods

run_regression <- function(data,formula) {
  model <- lm(formula, data = data)  # Specify your regression model formula here
  coef <- coef(model)  # Coefficients
  r2<-summary(model)$adj.r.squared
  return(data.frame(coef,r2))
}

fmreg<- function(data_split,formula){
  # Run regressions for each cross-sectional period
  regression_results <- lapply(data_split, run_regression,formula)
  
  # Combine the regression results into a single data frame
  results_df <- t(do.call(cbind, regression_results))
  
  r2s=results_df[seq(from=2,to=nrow(results_df),by=2),1]
  results_df<-results_df[seq(from=1,to=nrow(results_df)-1,by=2),]
  # Calculate the average coefficients and standard errors
  op<-matrix(NA, nrow = 4, ncol = ncol(results_df))
  for(i in 1:ncol(results_df)){
    modl=lm(results_df[,i]~ 1)
    sd = coeftest(modl)
    op[1,i] = sd[,1]
    op[2,i] = op[1,i]/sd[,2]
    op[3,i] = sd[,4]
  }
  op[4,1]<-mean(r2s)
  return(op)
}
op_tsim_mult<-fmreg(data_split,"Nextexret ~  tnic3tsimm + tnic3tsimm2 + (Age) + (Logat) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE)")
op_hhi_mult<-fmreg(data_split,"Nextexret ~  HHI +HHI2 + (Age) + (Logat)  + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE)")
op_tsim_multsing<-fmreg(data_split,"Nextexret ~  tnic3tsimm  + (Age) + (Logat) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE)")
op_hhi_multsing<-fmreg(data_split,"Nextexret ~  HHI  + (Age) + (Logat)  + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE)")
op_mult<-fmreg(data_split,"Nextexret ~  tnic3tsimm + tnic3tsimm2 +HHI +HHI2 + (Age) + (Logat)  + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE)")
#Bottom: R^2 and below t-stat:
Market_Paper = data.frame(Coeff_Names = rep(NA,2*ncol(op_mult)+1),ProdFluidSing = rep(NA,2*ncol(op_mult)+1),ProdFluidCom = rep(NA,2*ncol(op_mult)+1), 
                          HHISing = rep(NA,2*ncol(op_mult)+1),HHICom = rep(NA,2*ncol(op_mult)+1), MultiProdHHI = rep(NA,2*ncol(op_mult)+1))

Names = c("Const","tsim","tsim^2","log(HHI)","log(HHI)^2","log(Age)","log(AT)","Dividend","Financial~leverage","log(Firm~size)","Illiquidity","Investment~rate","iVol",
          "log(Mkt/Book)","Momentum","Operating~leverage","q","ROE")

Market_Paper$Coeff_Names[seq(from=1,to=(nrow(Market_Paper)-1),by=2)] = Names
Market_Paper$Coeff_Names[(nrow(Market_Paper))] = "R^2"


source("Table_Functions.R")
Market_Paper$ProdFluidSing[c(1,3,seq(from=11,to=(nrow(Market_Paper)-1),by=2))] = paste0(as.character(Lag_zero(round(op_tsim_multsing[1,],digits=3),digits=3)),star_function(op_tsim_multsing[3,]))
Market_Paper$ProdFluidSing[c(2,4,seq(from=12,to=(nrow(Market_Paper)-1),by=2))] = paste0("(",Lag_zero(round(op_tsim_multsing[2,],digits=3),digits=3),")")
Market_Paper$ProdFluidSing[nrow(Market_Paper)] = as.character(Lag_zero(round(op_tsim_multsing[4,1],digits=4) * 100,digits=2))

Market_Paper$ProdFluidCom[c(1,3,5,seq(from=11,to=(nrow(Market_Paper)-1),by=2))] = paste0(as.character(Lag_zero(round(op_tsim_mult[1,],digits=3),digits=3)),star_function(op_tsim_mult[3,]))
Market_Paper$ProdFluidCom[c(2,4,6,seq(from=12,to=(nrow(Market_Paper)-1),by=2))] = paste0("(",Lag_zero(round(op_tsim_mult[2,],digits=3),digits=3),")")
Market_Paper$ProdFluidCom[nrow(Market_Paper)] = as.character(Lag_zero(round(op_tsim_mult[4,1],digits=4) * 100,digits=2))

Market_Paper$HHISing[c(1,7,seq(from=11,to=(nrow(Market_Paper)-1),by=2))] = paste0(as.character(Lag_zero(round(op_hhi_multsing[1,],digits=3),digits=3)),star_function(op_hhi_multsing[3,]))
Market_Paper$HHISing[c(2,8,seq(from=12,to=(nrow(Market_Paper)-1),by=2))] = paste0("(",Lag_zero(round(op_hhi_multsing[2,],digits=3),digits=3),")")
Market_Paper$HHISing[nrow(Market_Paper)] = as.character(Lag_zero(round(op_hhi_multsing[4,1],digits=4) * 100,digits=2))

Market_Paper$HHICom[c(1,seq(from=7,to=(nrow(Market_Paper)-1),by=2))] = paste0(as.character(Lag_zero(round(op_hhi_mult[1,],digits=3),digits=3)),star_function(op_hhi_mult[3,]))
Market_Paper$HHICom[c(2,seq(from=8,to=(nrow(Market_Paper)-1),by=2))] = paste0("(",Lag_zero(round(op_hhi_mult[2,],digits=3),digits=3),")")
Market_Paper$HHICom[nrow(Market_Paper)] = as.character(Lag_zero(round(op_hhi_mult[4,1],digits=4) * 100,digits=2))

Market_Paper$MultiProdHHI[c(seq(from=1,to=(nrow(Market_Paper)-1),by=2))] = paste0(as.character(Lag_zero(round(op_mult[1,],digits=3),digits=3)),star_function(op_mult[3,]))
Market_Paper$MultiProdHHI[c(seq(from=2,to=(nrow(Market_Paper)-1),by=2))] = paste0("(",Lag_zero(round(op_mult[2,],digits=3),digits=3),")")
Market_Paper$MultiProdHHI[nrow(Market_Paper)] = as.character(Lag_zero(round(op_mult[4,1],digits=4) * 100,digits=2))

library(xtable)
print(xtable(Market_Paper),include.rownames=F,sanitize.text.function=function(x){x})

# END Table 9
##############################################

#######################################
# Table 1: Summary Statistics (Lines 1-7; the tail risk measures are in a separate code file)

Summary.Stats = data.frame(Names = c("tsimm","HHI","beta^M","beta^{CF}","beta^{DR}","beta^{Up}","beta^{Down}"), Nobs =rep(NA,7),
                           Mean = rep(NA,7), SD = rep(NA,7), Median = rep(NA,7), Min = rep(NA,7), Max = rep(NA,7),
                           Skewness = rep(NA,7), Kurtosis = rep(NA,7))
library(data.table)
setDT(Temp)
################### Summary Statistics  HHI/tnic3tsimm  ###################################
#install.packages("moments")
library(moments)
Summary.Stats[1,2:9] = c(
  length(Temp$tnic3tsimm)-sum(is.na(Temp$tnic3tsimm)),
  mean(Temp[,mean(tnic3tsimm,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,sd(tnic3tsimm,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,median(tnic3tsimm,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,min(tnic3tsimm,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,max(tnic3tsimm,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,skewness(tnic3tsimm,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,kurtosis(tnic3tsimm,na.rm=T),by=YearMonth]$V1,na.rm=T)
)

Summary.Stats[2,2:9] = c(
  length(Temp$HHI)-sum(is.na(Temp$HHI)),
  mean(Temp[,mean(HHI,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,sd(HHI,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,median(HHI,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,min(HHI,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,max(HHI,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,skewness(HHI,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,kurtosis(HHI,na.rm=T),by=YearMonth]$V1,na.rm=T)
)


Summary.Stats[3,2:9] = c(
  length(Temp$Weight_Beta_M)-sum(is.na(Temp$Weight_Beta_M)),
  mean(Temp[,mean(Weight_Beta_M,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,sd(Weight_Beta_M,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,median(Weight_Beta_M,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,min(Weight_Beta_M,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,max(Weight_Beta_M,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,skewness(Weight_Beta_M,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,kurtosis(Weight_Beta_M,na.rm=T),by=YearMonth]$V1,na.rm=T)
)
Summary.Stats[4,2:9] = c(
  length(Temp$Weight_Beta_CF)-sum(is.na(Temp$Weight_Beta_CF)),
  mean(Temp[,mean(Weight_Beta_CF,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,sd(Weight_Beta_CF,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,median(Weight_Beta_CF,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,min(Weight_Beta_CF,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,max(Weight_Beta_CF,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,skewness(Weight_Beta_CF,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,kurtosis(Weight_Beta_CF,na.rm=T),by=YearMonth]$V1,na.rm=T)
)
Summary.Stats[5,2:9] = c(
  length(Temp$Weight_Beta_DR)-sum(is.na(Temp$Weight_Beta_DR)),
  mean(Temp[,mean(Weight_Beta_DR,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,sd(Weight_Beta_DR,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,median(Weight_Beta_DR,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,min(Weight_Beta_DR,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,max(Weight_Beta_DR,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,skewness(Weight_Beta_DR,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,kurtosis(Weight_Beta_DR,na.rm=T),by=YearMonth]$V1,na.rm=T)
)
Summary.Stats[6,2:9] = c(
  length(Temp$Weight_Beta_Up)-sum(is.na(Temp$Weight_Beta_Up)),
  mean(Temp[,mean(Weight_Beta_Up,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,sd(Weight_Beta_Up,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,median(Weight_Beta_Up,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,min(Weight_Beta_Up,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,max(Weight_Beta_Up,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,skewness(Weight_Beta_Up,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,kurtosis(Weight_Beta_Up,na.rm=T),by=YearMonth]$V1,na.rm=T)
)
Summary.Stats[7,2:9] = c(
  length(Temp$Weight_Beta_Down)-sum(is.na(Temp$Weight_Beta_Down)),
  mean(Temp[,mean(Weight_Beta_Down,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,sd(Weight_Beta_Down,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,median(Weight_Beta_Down,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,min(Weight_Beta_Down,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,max(Weight_Beta_Down,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,skewness(Weight_Beta_Down,na.rm=T),by=YearMonth]$V1,na.rm=T),
  mean(Temp[,kurtosis(Weight_Beta_Down,na.rm=T),by=YearMonth]$V1,na.rm=T)
)

Summary.Stats[,3:9] = round(Summary.Stats[,3:9],digits=2)
library(xtable)
print(xtable(Summary.Stats),sanitize.text.function=function(x){x},include.rownames = F)
setDF(Temp)
# END Table 1
##############################################

#######################################
# Table 2: Correlations

A = cor(cbind(Temp[,c("tnic3tsimm")],(Temp[,c("HHI")]),(Temp[,c("Age")]),Temp[,c("Logat")],
              Temp[,c("Default_Spread")],Temp[,c("Dividend_Dummy")],Temp[,c("Financial_Leverage")],(Temp[,c("MktCap")])
              ,Temp[,c("Illiquidity")],Temp[,c("InvestmentRates")],Temp[,c("I_Vol")],(Temp[,c("Mkt_Book")]),Temp[,c("Momentum")]
              ,Temp[,c("Operating_Leverage")],Temp[,c("q")],Temp[,c("ROE")]),use="pairwise.complete.obs")
A[upper.tri(A)] = NA
print(xtable(A),row.names=F)
# END Table 2
##############################################

##########################
# Figure 1
library(data.table)


#New Plot:
setDT(Panel_New)
New = Panel_New[,sum(tnic3tsimm*sale,na.rm=T)/sum(sale,na.rm=T),by=year]
New = New[New$year>=1990,]

#Trial:
DT = New[New$year == 2010,]
Fabian <- function(DT){
  Reg = summary(lm(V1~1,DT))
  Down = coef(Reg)[1] - 1.645 * coef(Reg)[2]
  Mean =  coef(Reg)[1]
  Up = coef(Reg)[1] + 1.645 * coef(Reg)[2]
  A<-list(Down,Mean,Up)
  return(A)
}


New = New[order(New$year),]
New = na.omit(New)

library(ggplot2)
library(gridExtra)
library(grid)
library(zoo)
New = na.omit(New)
g11 = ggplot() + 
  geom_line(data=New,aes(x=year,y=V1)) + theme_light() + theme(plot.title = element_text(hjust = 0.5, size=16), axis.text.x = element_text(vjust = 0.5, size = 12), axis.text.y = element_text(vjust = 0.5, size = 12)) + labs(x = "Year", y = "Aggregate Total Similarity")
g11 = g11  + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(),axis.title=element_text(size=16)) + scale_y_continuous(limits = c(2,8)) + scale_x_continuous(breaks = c(1990,1995,2000,2005,2010,2015))
#Add NBER crisis
g11 = g11 + geom_rect(aes(xmin = 2000, xmax = 2001, ymax=Inf, ymin=-Inf),fill='grey',alpha=.25) + geom_rect(aes(xmin = 2007, xmax = 2009, ymax=Inf, ymin=-Inf),fill='grey',alpha=.25)
#Without MarkUps (plain)
#ggsave("Scaled_Prodmktfluid.pdf", g11, width = 12, height = 6) 
#Include the Graphic from De Loecker Mark-Ups
MarkUps<-read.table("US_Markups.csv",sep=";",dec=",")
MarkUps<-t(MarkUps)
MarkUps<-MarkUps[-1,]
MarkUps<-as.data.frame(MarkUps)
MarkUps[,1]<-as.numeric(MarkUps[,1])
MarkUps[,2]<-as.numeric(MarkUps[,2])
colnames(MarkUps)<-c("year","MarkUp")
#plot(MarkUps$year,MarkUps$MarkUp,type="l")
#Add a second scale to the previous plot and make the line grey (black and white friendly)
New<-merge(New,MarkUps,by="year",all.x=T,all.y = F)
#New = na.omit(New)
g11 = ggplot() + 
  geom_line(data=New,aes(x=year,y=V1)) + 
  geom_line(data=New,aes(x=year,y=(MarkUp)*4),col="gray45") + theme_light() + theme(plot.title = element_text(hjust = 0.5, size=16), axis.text.x = element_text(vjust = 0.5, size = 12), axis.text.y = element_text(vjust = 0.5, size = 12)) + labs(x = "Year", y = "Aggregate Total Similarity")
g11 = g11  + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(),axis.title=element_text(size=16)) + scale_y_continuous(limits = c(2,8), sec.axis = sec_axis(~.*0.25, name="Markup")) + scale_x_continuous(breaks = c(1990,1995,2000,2005,2010,2015))

#Add NBER crisis
g11 = g11 + geom_rect(aes(xmin = 2000, xmax = 2001, ymax=Inf, ymin=-Inf),fill='grey',alpha=.25) + geom_rect(aes(xmin = 2007, xmax = 2009, ymax=Inf, ymin=-Inf),fill='grey',alpha=.25)
#With MarkUps
ggsave("Scaled_Prodmktfluid_withMarkUps.pdf", g11, width = 12, height = 6) 
# END Figure 1
#########################




#######################################
# Table 3 and Table 4: Panel Regression Results

#Monthly and weighting
#standardize all controls
Temp$HHI=scale(log(Temp$HHI))
a<-poly(Temp$HHI,2)
Temp$HHI2=a[,2]/sd(a[,2])
Temp$prodmktfluid=scale(Temp$prodmktfluid)
a<-poly(Temp$prodmktfluid,2)
Temp$prodmktfluid2=a[,2]/sd(a[,2])
Temp$tnic3hhi=scale(Temp$tnic3hhi)
a<-poly(Temp$tnic3hhi,2)
Temp$tnic3hhi2=a[,2]/sd(a[,2])
Temp$tnic3tsimm=scale(Temp$tnic3tsimm)
a<-poly(Temp$tnic3tsimm,2)
Temp$tnic3tsimm2=a[,2]/sd(a[,2])
Temp$Age=scale(log(Temp$Age))
Temp$Momentum=scale(Temp$Momentum)
Temp$Illiquidity=scale(Temp$Illiquidity)
Temp$MktCap=scale(log(Temp$MktCap))
Temp$Mkt_Book=scale(log(Temp$Mkt_Book))
Temp$Financial_Leverage=scale(Temp$Financial_Leverage)
Temp$Operating_Leverage=scale(Temp$Operating_Leverage)
Temp$Default_Spread=scale(Temp$Default_Spread)
Temp$Firm_size=scale(Temp$Firm_size)
Temp$InvestmentRates=scale(Temp$InvestmentRates)
Temp$ROE=scale(Temp$ROE)
Temp$Logat=scale(Temp$Logat)
Temp$q=scale(Temp$q)
Temp$I_Vol=scale(Temp$I_Vol)

Tempearly = Temp[Temp$year<=2004,]
Template = Temp[Temp$year>=2005,]

#install.packages("fixest")
library(fixest)

A_Uni = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2| PERMNO + year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")

# predict the effect
model_feols <- feols(data = Temp, Weight_Beta_M ~  tnic3tsimm + I(tnic3tsimm^2) | PERMNO + year)
newdata=Temp[1:5,]
newdata$tnic3tsimm[1]<--2
newdata$tnic3tsimm[2]<--1
newdata$tnic3tsimm[3]<-0
newdata$tnic3tsimm[4]<-1
newdata$tnic3tsimm[5]<-2
predict(model_feols, newdata)
model_feols <- feols(data = Temp, Weight_Beta_M ~  HHI + I(HHI^2) | PERMNO + year)
newdata=Temp[1:5,]
newdata$HHI[1]<--2
newdata$HHI[2]<--1
newdata$HHI[3]<-0
newdata$HHI[4]<-1
newdata$HHI[5]<-2
predict(model_feols, newdata)

B_Uni = felm(Weight_Beta_M ~  HHI + HHI2| PERMNO + year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")
A_Uni_early = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2| PERMNO + year | 0 | NAICS+year, data = Tempearly, cmethod="reghdfe")
# predict the effect -> can simply do with the alternative model formulations with non-orthogonal squares --> the impact of this model is exactly the same!!!
model_feols <- feols(data = Tempearly, Weight_Beta_M ~  tnic3tsimm + I(tnic3tsimm^2) | PERMNO + year)
newdata=Tempearly[1:5,]
newdata$tnic3tsimm[1]<--2
newdata$tnic3tsimm[2]<--1
newdata$tnic3tsimm[3]<-0
newdata$tnic3tsimm[4]<-1
newdata$tnic3tsimm[5]<-2
predict(model_feols, newdata)

B_Uni_early = felm(Weight_Beta_M ~  HHI + HHI2| PERMNO + year | 0 | NAICS+year, data = Tempearly, cmethod="reghdfe")
A_Uni_late = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2| PERMNO + year | 0 | NAICS+year, data = Template, cmethod="reghdfe")
# predict the effect
model_feols <- feols(data = Template, Weight_Beta_M ~  tnic3tsimm + I(tnic3tsimm^2) | PERMNO + year)
newdata=Template[1:5,]
newdata$tnic3tsimm[1]<--2
newdata$tnic3tsimm[2]<--1
newdata$tnic3tsimm[3]<-0
newdata$tnic3tsimm[4]<-1
newdata$tnic3tsimm[5]<-2
predict(model_feols, newdata)


B_Uni_late = felm(Weight_Beta_M ~  HHI + HHI2| PERMNO + year | 0 | NAICS+year, data = Template, cmethod="reghdfe")


A_Uni_year = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2| year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")
B_Uni_year = felm(Weight_Beta_M ~  HHI + HHI2| year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")
A_Uni_no = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2| 0 | 0 | NAICS+year, data = Temp, cmethod="reghdfe")
B_Uni_no = felm(Weight_Beta_M ~  HHI + HHI2| 0 | 0 | NAICS+year, data = Temp, cmethod="reghdfe")
A_Uni_permno = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2| PERMNO | 0 | NAICS+year, data = Temp, cmethod="reghdfe")
B_Uni_permno = felm(Weight_Beta_M ~  HHI + HHI2| PERMNO | 0 | NAICS+year, data = Temp, cmethod="reghdfe")

A_Complete = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")

B_Complete = felm(Weight_Beta_M ~  HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")

AB_Complete = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2 + HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                     (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                     (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")

A_Complete_early = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Tempearly, cmethod="reghdfe")

B_Complete_early = felm(Weight_Beta_M ~  HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Tempearly, cmethod="reghdfe")

AB_Complete_early = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2 + HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                     (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                     (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Tempearly, cmethod="reghdfe")

A_Complete_late = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Template, cmethod="reghdfe")

B_Complete_late = felm(Weight_Beta_M ~  HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Template, cmethod="reghdfe")

AB_Complete_late = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2 + HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                     (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                     (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Template, cmethod="reghdfe")


table_op <- function(A_Unitouse,B_Unitouse,A_Completetouse,B_Completetouse,AB_Completetouse){
#Bottom: R^2 and below t-stat:
Market_Paper = data.frame(Coeff_Names = rep(NA,2*length(coef(AB_Completetouse))+2),ProdFluid = rep(NA,2*length(coef(AB_Completetouse))+2), 
                          HHI = rep(NA,2*length(coef(AB_Completetouse))+2), ProdFluidCom = rep(NA,2*length(coef(AB_Completetouse))+2), 
                          HHICom = rep(NA,2*length(coef(AB_Completetouse))+2), MultiProdHHI = rep(NA,2*length(coef(AB_Completetouse))+2))

Names = c("tsim","tsim^2","log(HHI)","log(HHI)^2","log(Age)","log(AT)","Default~spread","Dividend","Financial~leverage","log(Firm~size)","Illiquidity","Investment~rate","iVol",
          "log(Mkt/Book)","Momentum","Operating~leverage","q","ROE")

Market_Paper$Coeff_Names[seq(from=1,to=(nrow(Market_Paper)-2),by=2)] = Names
Market_Paper$Coeff_Names[(nrow(Market_Paper)-1)] = "R^2"
Market_Paper$Coeff_Names[(nrow(Market_Paper))] = "Fixed-Effects"

Market_Paper$ProdFluid[c(1,3)] = paste0(as.character(Lag_zero(round(summary(A_Unitouse)$coefficients[,1],digits=3),digits=3)),star_function(summary(A_Unitouse)$coefficients[,4]))
Market_Paper$ProdFluid[c(2,4)] = paste0("(",Lag_zero(round(summary(A_Unitouse)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$ProdFluid[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(A_Unitouse)$r2adj,digits=4) * 100,digits=2))
Market_Paper$ProdFluid[nrow(Market_Paper)] = "Yes"

Market_Paper$HHI[c(5,7)] = paste0(as.character(Lag_zero(round(summary(B_Unitouse)$coefficients[,1],digits=3),digits=3)),star_function(summary(B_Unitouse)$coefficients[,4]))
Market_Paper$HHI[c(6,8)] = paste0("(",Lag_zero(round(summary(B_Unitouse)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$HHI[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(B_Unitouse)$r2adj,digits=4) * 100,digits=2))
Market_Paper$HHI[nrow(Market_Paper)] = "Yes"

Market_Paper$ProdFluidCom[c(1,3,seq(from=9,to=(nrow(Market_Paper)-2),by=2))] = paste0(as.character(Lag_zero(round(summary(A_Completetouse)$coefficients[,1],digits=3),digits=3)),star_function(summary(A_Completetouse)$coefficients[,4]))
Market_Paper$ProdFluidCom[c(2,4,seq(from=10,to=(nrow(Market_Paper)-2),by=2))] = paste0("(",Lag_zero(round(summary(A_Completetouse)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$ProdFluidCom[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(A_Completetouse)$r2adj,digits=4) * 100,digits=2))
Market_Paper$ProdFluidCom[nrow(Market_Paper)] = "Yes"

Market_Paper$HHICom[seq(from=5,to=(nrow(Market_Paper)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(B_Completetouse)$coefficients[,1],digits=3),digits=3)),star_function(summary(B_Completetouse)$coefficients[,4]))
Market_Paper$HHICom[seq(from=6,to=(nrow(Market_Paper)-2),by=2)] = paste0("(",Lag_zero(round(summary(B_Completetouse)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$HHICom[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(B_Completetouse)$r2adj,digits=4) * 100,digits=2))
Market_Paper$HHICom[nrow(Market_Paper)] = "Yes"

Market_Paper$MultiProdHHI[seq(from=1,to=(nrow(Market_Paper)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(AB_Completetouse)$coefficients[,1],digits=3),digits=3)),star_function(summary(AB_Completetouse)$coefficients[,4]))
Market_Paper$MultiProdHHI[seq(from=2,to=(nrow(Market_Paper)-2),by=2)] = paste0("(",Lag_zero(round(summary(AB_Completetouse)$coefficients[,3],digits=3),digits=3),")")
Market_Paper$MultiProdHHI[nrow(Market_Paper)-1] = as.character(Lag_zero(round(summary(AB_Completetouse)$r2adj,digits=4) * 100,digits=2))
Market_Paper$MultiProdHHI[nrow(Market_Paper)] = "Yes"

library(xtable)
print(xtable(Market_Paper),include.rownames=F,sanitize.text.function=function(x){x})
}

table_op(A_Unitouse=A_Uni,B_Unitouse=B_Uni,A_Completetouse=A_Complete,B_Completetouse=B_Complete,AB_Completetouse=AB_Complete)
table_op(A_Unitouse=A_Uni_early,B_Unitouse=B_Uni_early,A_Completetouse=A_Complete_early,B_Completetouse=B_Complete_early,AB_Completetouse=AB_Complete_early)
table_op(A_Unitouse=A_Uni_late,B_Unitouse=B_Uni_late,A_Completetouse=A_Complete_late,B_Completetouse=B_Complete_late,AB_Completetouse=AB_Complete_late)

# END Tables 3 and 4
##############################

############################################################
# Table 10: Robustness Tests

A = felm(Beta_M ~  HHI + HHI2 + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy  + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol)| PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[1,1] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[2,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[3,1] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[4,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[5,1] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Beta_M ~  prodmktfluid + prodmktfluid2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol)| PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[6,1] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[7,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[8,1] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[9,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[10,1] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Beta_M ~  tnic3hhi + tnic3hhi2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy  + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol)| PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[11,1] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[12,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[13,1] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[14,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[15,1] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Beta_M ~  tnic3tsimm + tnic3tsimm2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[16,1] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[17,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[18,1] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[19,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[20,1] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)


###
A = felm(Weight_Beta_M ~  HHI + HHI2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[1,6] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[2,6] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[3,6] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[4,6] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[5,6] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Weight_Beta_M ~  prodmktfluid + prodmktfluid2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[6,6] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[7,6] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[8,6] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[9,6] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[10,6] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Weight_Beta_M ~  tnic3hhi + tnic3hhi2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[11,6] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[12,6] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[13,6] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[14,6] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[15,6] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Weight_Beta_M ~  tnic3tsimm + tnic3tsimm2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[16,6] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[17,6] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[18,6] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[19,6] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[20,6] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)


###
A = felm(Shrinking_Beta_M ~  HHI + HHI2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[1,11] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[2,11] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[3,11] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[4,11] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[5,11] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Shrinking_Beta_M ~  prodmktfluid + prodmktfluid2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[6,11] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[7,11] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[8,11] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[9,11] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[10,11] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Shrinking_Beta_M ~  tnic3hhi + tnic3hhi2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[11,11] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[12,11] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[13,11] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[14,11] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[15,11] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Shrinking_Beta_M ~  tnic3tsimm + tnic3tsimm2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[16,11] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[17,11] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[18,11] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[19,11] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[20,11] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)


###
A = felm(Weighting_Shrinking_Beta_M ~  HHI + HHI2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[1,16] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[2,16] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[3,16] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[4,16] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[5,16] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Weighting_Shrinking_Beta_M ~  prodmktfluid + prodmktfluid2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[6,16] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[7,16] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[8,16] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[9,16] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[10,16] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Weighting_Shrinking_Beta_M ~  tnic3hhi + tnic3hhi2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[11,16] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[12,16] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[13,16] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[14,16] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[15,16] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

A = felm(Weighting_Shrinking_Beta_M ~  tnic3tsimm + tnic3tsimm2  + ((Age)) + (Logat) + (Momentum) + (Illiquidity) + ((Mkt_Book)) + 
           (Default_Spread) + Dividend_Dummy + ((MktCap)) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[16,16] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[17,16] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[18,16] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[19,16] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[20,16] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)



library(xtable)
#Only Market Beta:
print(xtable(Market_Power[,c(1,6,11,16)]),sanitize.text.function=function(x){x})

# END Table 10
######################

############################################################
# Table 7: Partial Betas
Partial_Paper = data.frame(Coeff_Names = rep(NA,2*length(coef(AB_Complete))+2),Beta_CF = rep(NA,2*length(coef(AB_Complete))+2), 
                           Beta_DR = rep(NA,2*length(coef(AB_Complete))+2), Beta_Up = rep(NA,2*length(coef(AB_Complete))+2), 
                           Beta_Down = rep(NA,2*length(coef(AB_Complete))+2))

B_Complete = felm(Weight_Beta_CF ~  tnic3tsimm + tnic3tsimm2 + HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")

C_Complete = felm(Weight_Beta_DR ~  tnic3tsimm + tnic3tsimm2 + HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")

D_Complete = felm(Weight_Beta_Up ~  tnic3tsimm + tnic3tsimm2 + HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")

E_Complete = felm(Weight_Beta_Down ~  tnic3tsimm + tnic3tsimm2 + HHI + HHI2 + (Age) + (Logat) + (Default_Spread) + (Dividend_Dummy) + (Financial_Leverage) + 
                    (MktCap) + (Illiquidity) + (InvestmentRates) + (I_Vol) + (Mkt_Book) + (Momentum) +
                    (Operating_Leverage) + (q) + (ROE) | PERMNO + year | 0 | NAICS+year, data = Temp, cmethod="reghdfe")
Names = c("tsim","tsim^2","log(HHI)","log(HHI)^2","log(Age)","log(AT)","Default~spread","Dividend","Financial~leverage","log(Firm~size)","Illiquidity","Investment~rate","iVol",
          "log(Mkt/Book)","Momentum","Operating~leverage","q","ROE")

Partial_Paper$Coeff_Names[seq(from=1,to=(nrow(Partial_Paper)-2),by=2)] = Names
Partial_Paper$Coeff_Names[nrow(Partial_Paper)-1] = "R^2"
Partial_Paper$Coeff_Names[nrow(Partial_Paper)] = "Fixed-Effects"

Partial_Paper$Beta_CF[seq(from=1,to=(nrow(Partial_Paper)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(B_Complete)$coefficients[,1],digits=3),digits=3)),star_function(summary(B_Complete)$coefficients[,4]))
Partial_Paper$Beta_CF[seq(from=2,to=(nrow(Partial_Paper)-2),by=2)] = paste0("(",Lag_zero(round(summary(B_Complete)$coefficients[,3],digits=3),digits=3),")")
Partial_Paper$Beta_CF[nrow(Partial_Paper)-1] = as.character(Lag_zero(round(summary(B_Complete)$r2adj,digits=4) * 100,digits=2))
Partial_Paper$Beta_CF[nrow(Partial_Paper)] = "Yes"

Partial_Paper$Beta_DR[seq(from=1,to=(nrow(Partial_Paper)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(C_Complete)$coefficients[,1],digits=3),digits=3)),star_function(summary(C_Complete)$coefficients[,4]))
Partial_Paper$Beta_DR[seq(from=2,to=(nrow(Partial_Paper)-2),by=2)] = paste0("(",Lag_zero(round(summary(C_Complete)$coefficients[,3],digits=3),digits=3),")")
Partial_Paper$Beta_DR[nrow(Partial_Paper)-1] = as.character(Lag_zero(round(summary(C_Complete)$r2adj,digits=4) * 100,digits=2))
Partial_Paper$Beta_DR[nrow(Partial_Paper)] = "Yes"

Partial_Paper$Beta_Up[seq(from=1,to=(nrow(Partial_Paper)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(D_Complete)$coefficients[,1],digits=3),digits=3)),star_function(summary(D_Complete)$coefficients[,4]))
Partial_Paper$Beta_Up[seq(from=2,to=(nrow(Partial_Paper)-2),by=2)] = paste0("(",Lag_zero(round(summary(D_Complete)$coefficients[,3],digits=3),digits=3),")")
Partial_Paper$Beta_Up[nrow(Partial_Paper)-1] = as.character(Lag_zero(round(summary(D_Complete)$r2adj,digits=4) * 100),digits=2)
Partial_Paper$Beta_Up[nrow(Partial_Paper)] = "Yes"

Partial_Paper$Beta_Down[seq(from=1,to=(nrow(Partial_Paper)-2),by=2)] = paste0(as.character(Lag_zero(round(summary(E_Complete)$coefficients[,1],digits=3),digits=3)),star_function(summary(E_Complete)$coefficients[,4]))
Partial_Paper$Beta_Down[seq(from=2,to=(nrow(Partial_Paper)-2),by=2)] = paste0("(",Lag_zero(round(summary(E_Complete)$coefficients[,3],digits=3),digits=3),")")
Partial_Paper$Beta_Down[nrow(Partial_Paper)-1] = as.character(Lag_zero(round(summary(E_Complete)$r2adj,digits=4) * 100,digits=2))
Partial_Paper$Beta_Down[nrow(Partial_Paper)] = "Yes"

print(xtable(Partial_Paper),include.rownames=F,sanitize.text.function=function(x){x})

# END Table 7
######################


######################################
# Table 10 (missing part): Robustness fitted HHI 
load("Control_Panel.RData")
Temp = Panel_New[,c("Time","YearMonth","year","NAICS","PERMNO","Beta_M","Beta_CF","Beta_DR","Beta_Down","Beta_Up","Weight_Beta_M","Weight_Beta_CF","Weight_Beta_DR",
                    "Weight_Beta_Up","Weight_Beta_Down","Shrinking_Beta_M","Shrinking_Beta_CF","Shrinking_Beta_DR","Shrinking_Beta_Up","Shrinking_Beta_Down",
                    "Weighting_Shrinking_Beta_M","Weighting_Shrinking_Beta_CF","Weighting_Shrinking_Beta_DR","Weighting_Shrinking_Beta_Up","Weighting_Shrinking_Beta_Down","fithhi",
                    "Age","Momentum","Illiquidity","MktCap","Mkt_Book","Default_Spread","Financial_Leverage", "Operating_Leverage","Firm_size","InvestmentRates","ROE","Logat","Dividend_Dummy","q","I_Vol")]#

for(i in 1:dim(Temp)[2]){
  Temp[,i][Temp[,i] == Inf] = NA
  Temp[,i][Temp[,i] == -Inf] = NA
}
Temp = na.omit(Temp)
Temp = Temp[Temp$year>=1989,]
Temp = Temp[Temp$year<2006,]

Temp$fithhi=scale(log(Temp$fithhi))
a<-poly(Temp$fithhi,2)
Temp$fithhi2=a[,2]/sd(a[,2])
Temp$Age=scale(log(Temp$Age))
Temp$Momentum=scale(Temp$Momentum)
Temp$Illiquidity=scale(Temp$Illiquidity)
Temp$MktCap=scale(log(Temp$MktCap))
Temp$Mkt_Book=scale(log(Temp$Mkt_Book))
Temp$Financial_Leverage=scale(Temp$Financial_Leverage)
Temp$Operating_Leverage=scale(Temp$Operating_Leverage)
Temp$Default_Spread=scale(Temp$Default_Spread)
Temp$Firm_size=scale(Temp$Firm_size)
Temp$InvestmentRates=scale(Temp$InvestmentRates)
Temp$ROE=scale(Temp$ROE)
Temp$Logat=scale(Temp$Logat)
Temp$q=scale(Temp$q)
Temp$I_Vol=scale(Temp$I_Vol)

library(lfe)

Market_Power = data.frame(Beta_M = rep(NA,5),Weighting_Beta_M = rep(NA,5),
                          Shrinking_Beta_M = rep(NA,5),Weighting_Shrinking_Beta_M = rep(NA,5))

rownames(Market_Power) = c("(fithhi)","t-stat 1","HHI^2","t-stat 1.1","R_sq 1")


A = felm(Beta_M ~  fithhi + fithhi2  + (Age) + (Logat) + (Momentum) + (Illiquidity) + (Mkt_Book) + 
           (Default_Spread) + Dividend_Dummy + (MktCap) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[1,1] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[2,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[3,1] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[4,1] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[5,1] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)


A = felm(Weight_Beta_M ~  fithhi + fithhi2  + (Age) + (Logat) + (Momentum) + (Illiquidity) + (Mkt_Book) + 
           (Default_Spread) + Dividend_Dummy + (MktCap) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[1,2] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[2,2] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[3,2] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[4,2] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[5,2] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)


A = felm(Shrinking_Beta_M ~  fithhi + fithhi2  + (Age) + (Logat) + (Momentum) + (Illiquidity) + (Mkt_Book) + 
           (Default_Spread) + Dividend_Dummy + (MktCap) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[1,3] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[2,3] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[3,3] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[4,3] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[5,3] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)


A = felm(Weighting_Shrinking_Beta_M ~  fithhi + fithhi2  + (Age) + (Logat) + (Momentum) + (Illiquidity) + (Mkt_Book) + 
           (Default_Spread) + Dividend_Dummy + (MktCap) + (InvestmentRates) + 
           (ROE) + (Operating_Leverage) + (Financial_Leverage) + (q) + (I_Vol) | PERMNO + year | 0 | NAICS + year, data = Temp, cmethod="reghdfe")

Market_Power[1,4] = paste0(Lag_zero(round(summary(A)$coefficients[1,1],digits=3),digits=3),star_function(summary(A)$coefficients[1,4]))
Market_Power[2,4] = paste0("(",Lag_zero(round(summary(A)$coefficients[1,3],digits=3),digits=3),")")
Market_Power[3,4] = paste0(Lag_zero(round(summary(A)$coefficients[2,1],digits=3),digits=3),star_function(summary(A)$coefficients[2,4]))
Market_Power[4,4] = paste0("(",Lag_zero(round(summary(A)$coefficients[2,3],digits=3),digits=3),")")
Market_Power[5,4] = Lag_zero(round(summary(A)$r2adj,digits=4) * 100,digits=2)

print(xtable(Market_Power),include.rownames=F,sanitize.text.function=function(x){x})

# END Table 10 (missing part)
###############################################

